package com.itlong.cloud.controller.old;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itlong.cloud.POJO.DTO.app.old.AppOldIosDeviceListDTO;
import com.itlong.cloud.POJO.DTO.app.old.AppOldUserInfoDto;
import com.itlong.cloud.POJO.VO.app.old.AppOldAdVo;
import com.itlong.cloud.POJO.VO.app.old.AppOldDeviceVO;
import com.itlong.cloud.POJO.VO.app.old.AppOldPushVO;
import com.itlong.cloud.enumerate.oldapp.Code;
import com.itlong.cloud.enumerate.oldapp.Message;
import com.itlong.cloud.equip.app.IAppDeviceServiceClient;
import com.itlong.cloud.project.app.IAppProjectServiceClient;
import com.itlong.cloud.user.app.IAppOldHomeServiceClient;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *     旧版APP首页版块控制器
 * </desc>
 *
 * Created 2017/10/11
 */
@RestController
@RequestMapping("/mobile/interface")
public class AppOldHomeController {
    private static final Logger LOG = LoggerFactory.getLogger(AppOldHomeController.class);

    @Autowired
    IAppOldHomeServiceClient iAppOldHomeServiceClient;

    @Autowired
    IAppDeviceServiceClient iAppDeviceServiceClient;

    @Autowired
    IAppProjectServiceClient iAppProjectServiceClient;
    /**
     * <desc>
     *     旧版APP_home开门首页
     * </desc>
     * @param pushAutoIndex
     * @param phoneNo
     * @param pageSize
     * @param pageNo
     * @param projectId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(path = "/app/home")
    public Message appHome(String pushAutoIndex,String phoneNo,Integer pageSize,Integer pageNo,String projectId) throws Exception{
        //LOG.info("【旧版APP_旧版APP_home开门首页】");
        AppOldUserInfoDto appOldUserInfoDto = iAppOldHomeServiceClient.getAppOldUserDtoByPhone(phoneNo);
        if (appOldUserInfoDto == null){
            return new Message(Code.NOT_PARAM);
        }
        if (appOldUserInfoDto != null || StringUtils.isBlank(projectId)){
            if (appOldUserInfoDto.getUserStatus() != 1){
                return new Message(Code.USER_STATUSERR);
            }
        }
        AppOldPushVO appOldPushVO = iAppOldHomeServiceClient.getAppOldPushVO(appOldUserInfoDto.getUserId(), pushAutoIndex, projectId);
        List<AppOldAdVo> adList = iAppOldHomeServiceClient.getAppOldAd(appOldUserInfoDto.getUserId(), projectId);
        JSONObject data = new JSONObject();
        if(StringUtils.isNotBlank(projectId)){
            JSONArray jsonArray=iAppOldHomeServiceClient.getHomeDeviceByProjectId(appOldUserInfoDto.getUserId(), projectId);
            data.put("deviceInfos",jsonArray);
        }else{
            List<AppOldDeviceVO> dls=new ArrayList<>();
            //查询用户拥有的项目列表
            List<String> userProjectList = iAppOldHomeServiceClient.getUserProjectList(appOldUserInfoDto.getUserId());
            for(String project:userProjectList){
                JSONArray jsonArray=iAppOldHomeServiceClient.getHomeDeviceByProjectId(appOldUserInfoDto.getUserId(),project);
                for(int i=0;i<jsonArray.size();i++){
                    List<AppOldDeviceVO> dl=(List<AppOldDeviceVO>)jsonArray.getJSONObject(i).get("deviceinfoArr");
                    dl.forEach(vo-> {
                        if (StringUtils.isNotBlank(vo.getDoorAuth())){
                            vo.setDoorAuth(vo.getDoorAuth().toUpperCase());
                        }
                        if (StringUtils.isNotBlank(vo.getFloorAuth())){
                            vo.setFloorAuth(vo.getFloorAuth().toUpperCase());
                        }
                        dls.add(vo);
                    });
                }
            }
            data.put("deviceInfos",dls);
        }
        data.put("pushInfo", appOldPushVO);
        data.put("ads",adList);
        return  new Message(Code.SUCCESS,data);
    }


    /**
     * <desc>
     *     旧版APP_open开门首页
     * </desc>
     * @param pushAutoIndex
     * @param phoneNo
     * @param pageSize
     * @param pageNo
     * @param projectId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(path = "/app/open")
    public Message appOpen(String pushAutoIndex, String phoneNo,Integer pageSize, Integer pageNo,String projectId)throws Exception {
        //LOG.info("【旧版APP_旧版APP_open开门首页】");
        AppOldUserInfoDto appOldUserInfoDto = iAppOldHomeServiceClient.getAppOldUserDtoByPhone(phoneNo);
        if (appOldUserInfoDto == null){
            return new Message(Code.NOT_PARAM);
        }
        if (appOldUserInfoDto != null){
            if (appOldUserInfoDto.getUserStatus() != 1){
                return new Message(Code.USER_STATUSERR);
            }
        }

        AppOldPushVO appOldPushVO = iAppOldHomeServiceClient.getAppOldPushVO(appOldUserInfoDto.getUserId(), pushAutoIndex, projectId);
        List<AppOldAdVo> adList = iAppOldHomeServiceClient.getAppOldAd(appOldUserInfoDto.getUserId(), projectId);
        if (adList != null&&adList.size()>0){
            for (AppOldAdVo appOldAdVo:adList){
                appOldAdVo = (AppOldAdVo) StringHandlerUtil.formatString(appOldAdVo);
                appOldAdVo.setStatus(1);
                appOldAdVo.setCreateTime(new Date());
            }
        }
        Message message = iAppOldHomeServiceClient.appOpen(pushAutoIndex,phoneNo,pageSize,pageNo,projectId);
        ((Map<String,Object>)message.getData()).put("pushInfo", appOldPushVO);
        ((Map<String,Object>)message.getData()).put("ads",adList);
        Map<String,Object> map = ((Map<String, Object>) message.getData());
        List<Map<String,Object>> projectArray=(List<Map<String,Object>>)map.get("deviceInfos");
        for(Map<String,Object> project:projectArray){
            AppOldIosDeviceListDTO dto=new AppOldIosDeviceListDTO();
            dto.setUserId(appOldUserInfoDto.getUserId());
            dto.setProjectId(StringHandlerUtil.getNullOrStringForObject(project.get("projectId")));
            dto.setCardNum(StringHandlerUtil.getNullOrStringForObject(project.get("cardNum")));
            dto.setPubKey(StringHandlerUtil.getNullOrStringForObject(project.get("pubKey")));
            dto.setSipAddress(StringHandlerUtil.getNullOrStringForObject(project.get("sipAddress")));
            dto.setSipPort(StringHandlerUtil.getNullOrStringForObject(project.get("sipPort")));
            dto.setUserSipAccount(StringHandlerUtil.getNullOrStringForObject(project.get("userSipAccount")));
            dto.setUserSipPsd(StringHandlerUtil.getNullOrStringForObject(project.get("userSipPsd")));
            project.put("deviceinfoArr",iAppOldHomeServiceClient.getIosDeviceList(dto));
        }
        map.put("deviceInfos",projectArray);
        message.setData(map);
        return message;
    }

    /**
     * <desc>
     *     旧版APP_查询用户信息
     * </desc>
     * @param phoneNo
     * @param projectId
     * @param pswd
     * @param loginDevice
     * @param token
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(path = "/app/getUserInfo")
    public Message getUserInfo(String phoneNo,String projectId,String pswd,String loginDevice,String token) throws Exception{
        //LOG.info("【旧版APP_旧版APP_查询用户信息】--"+phoneNo+"--"+projectId+"--"+pswd+"--"+loginDevice+"--"+token);
        Message message = iAppOldHomeServiceClient.getUserInfo(phoneNo,projectId,pswd,loginDevice,token);
        return message;
    }


















}
